<?php
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'WSL/FormValidator/FormValidator.php';

class WSL_FormValidator_Test extends PHPUnit_Framework_TestCase
{
    protected $_validator;
    
    public function setUp()
    {   
        $this->_validator = new WSL_FormValidator();
    }
    
    public function testgetAvailableRules()
    {
    	$size = count($this->_validator->getAvailableRules());

    	$this->assertGreaterThan(0, $size);
    }
    
    public function testForm1()
    {
    	/**
    	 * A form containing two fields:
    	 * - Name (it cannot be null or longer than 15 characters) 
    	 * - Age (it cannot be less than 0 or greater than 65)
    	 */
    	$this->setUp();
    	
        $this->_validator->addRequirement('name',
                                          'The name cannot be null',
                                          'NoEmpty');
        $this->_validator->addRequirement('name',
                                          'The name cannot be longer than 15',
                                          'ShorterThan', 15);
        $this->_validator->addRequirement('age',
                                          'The age cannot be negative',
                                          'GreaterThan', 0);
        $this->_validator->addRequirement('age',
                                          'The age cannot be greater than 65',
                                          'LessThan', 65);
        
        /**
         * Data:
         * - Name: Joe
         * - Age:  23
         * - Expected result: No errors
         */
        $data = array('name' => 'Joe',
                      'age'  => 23);
        
        $errors = $this->_validator->validate($data);
        
        $this->assertTrue(empty($errors));
        
        /**
         * Data:
         * - Name: null
         * - Age:  23
         * - Expected result: The name cannot be null
         */
        $data = array('name' => null,
                      'age'  => 23);
        
        $errors = $this->_validator->validate($data);

        $this->assertFalse(empty($errors['name']));
        $this->assertEquals('The name cannot be null', $errors['name'][0]);

        /**
         * Data:
         * - Name: Joe
         * - Age:  75
         * - Expected result: The age cannot be greater than 65
         */
        $data = array('name' => 'Joe',
                      'age'  => 75);
        
        $errors = $this->_validator->validate($data);

        $this->assertTrue(empty($errors['name']));
        $this->assertFalse(empty($errors['age']));
        $this->assertEquals('The age cannot be greater than 65', $errors['age'][0]);
        
        /**
         * Data:
         * - Name: Joe
         * - Age:  -23
         * - Expected result: The age cannot be negative
         */
        $data = array('name' => 'Joe',
                      'age'  => -23);
        
        $errors = $this->_validator->validate($data);

        $this->assertTrue(empty($errors['name']));
        $this->assertFalse(empty($errors['age']));
        $this->assertEquals('The age cannot be negative', $errors['age'][0]);
    }
    
    public function testForm2()
    {
    	/**
    	 * A form containing two fields:
    	 * - Blog (it needs to be a valid URL) 
    	 * - Email (it needs to be a valid Email address)
    	 */
    	$this->setUp();
    	
        $this->_validator->addRequirement('blog',
                                          'Please insert a valid URL',
                                          'Url');
        $this->_validator->addRequirement('email',
                                          'Please insert a valid email address',
                                          'Email');

        /**
         * Data:
         * - Blog:  http://www.xataka.com
         * - Email: xataka@gmail.com 
         * - Expected result: No errors
         */
        $data = array('blog'  => 'http://www.xataka.com',
                      'email' => 'xataka@gmail.com');
        
        $errors = $this->_validator->validate($data);

        $this->assertTrue(empty($errors));
        
        /**
         * Data:
         * - Blog:  xataka.moc
         * - Email: xataka@gmail.com 
         * - Expected result: Invalid URL
         */
        $data = array('blog'  => 'xataka.moc',
                      'email' => 'xataka@gmail.com');
        
        $errors = $this->_validator->validate($data);

        $this->assertTrue(empty($errors['email']));
        $this->assertFalse(empty($errors['blog']));
        $this->assertEquals('Please insert a valid URL', $errors['blog'][0]);
        
        /**
         * Data:
         * - Blog:  http://www.xataka.com
         * - Email: xatakagmail.com
         * - Expected result: Invalid Email address
         */
        $data = array('blog'  => 'http://www.xataka.com',
                      'email' => 'xatakagmail.com');
        
        $errors = $this->_validator->validate($data);

        $this->assertFalse(empty($errors['email']));
        $this->assertTrue(empty($errors['blog']));
        $this->assertEquals('Please insert a valid email address', $errors['email'][0]);
    }

    public function tearDown()
    {
        unset($this->_validator);
    }

}